﻿using MesBSW.BLL.Role;
using MesBSW.DAL.RoleDB;
using MesBSW.Tools;
using Sunny.UI;
using System;

namespace MesBSW.View.Dialog.Role
{
    public partial class AddMenuDia : UIEditForm
    {

        private MenuControler _my_ctrl;

        private BaseMenuModel menuModel;

        public AddMenuDia()
        { 
            InitializeComponent();
        }

        public AddMenuDia(MenuControler _my_ctrl, BaseMenuModel menuModel=null)
        {
            InitializeComponent();

            this._my_ctrl = _my_ctrl;

            if (menuModel != null)
            {
                this.menuModel = menuModel;
            }
            else
            {
                this.menuModel = new BaseMenuModel();
            }

            InitDiaValue();
        }

        private void InitDiaValue() 
        {
            this.ComboBoxMenus.DisplayMember = "menu_name";
            this.ComboBoxMenus.ValueMember = "id";
            this.ComboBoxFrm.DisplayMember = "name";
            this.ComboBoxFrm.ValueMember = "id";
            var MenuData  =  _my_ctrl.getMenus();
            MenuData.Insert(0, new BaseMenuModel { id = 0, menu_name = "父级" });
            this.ComboBoxMenus.DataSource = MenuData.ToDataTable();
            this.ComboBoxFrm.DataSource = _my_ctrl.getFrm().ToDataTable();
            
            if (this.menuModel.id!=0) 
            {
                this.menu_name.Text = this.menuModel.menu_name;  
                this.ComboBoxMenus.SelectedValue = this.menuModel.parent_id;
                this.ComboBoxMenus.Text = MenuData.Find(u=>u.id == this.menuModel.parent_id).menu_name;
                this.ComboBoxFrm.SelectedValue = this.menuModel.class_name;
                this.ComboBoxFrm.Text = this.menuModel.class_name;
                this.uiLabelSort.Text = this.menuModel.sort.ToString();  
            }     
        }

        public BaseMenuModel MenuModel
        {
            get
            {
                if (this.ComboBoxMenus.SelectedValue != null)
                {
                    menuModel.parent_id = this.ComboBoxMenus.SelectedValue.ToString().ToInt();
                }
                else 
                {
                    menuModel.parent_id = menuModel.parent_id;
                }

                if (this.ComboBoxFrm.SelectedValue != null)
                {
                    menuModel.class_name = this.ComboBoxFrm.SelectedValue.ToString();
                }
                else 
                {
                    menuModel.class_name = menuModel.class_name;
                }                                                                                   

                menuModel.menu_name = this.menu_name.Text.ToString();
                menuModel.sort = this.uiLabelSort.Text.ToInt();
                return menuModel;
            }
        }


        protected override bool CheckData()
        {
            return CheckEmpty(menu_name, "请输入菜单名称")
                   &&CheckEmpty(ComboBoxMenus,"请选择父项菜单");
        }



        private void btnOK_Click(object sender, EventArgs e)
        {
            _my_ctrl.addMenus(MenuModel);
            UIMessageBox.Show(SysTools.errMsg);
        }
    }
    
}
